<template>
  <div class="all">
    <div class="group">
      <GroupItem v-for="(item, index) in group" :key="index" :item="item" @deleteGroupItem="deleteGroupItem"/>
      <div class="add-item" @click.stop="showBottom = true;text='';">
        <van-icon class="icon-add" name="add" />
        <div class="text">创建分组</div>
      </div>
    </div>
    <van-popup
      @click-close-icon="addGroup"
      :closeable="true"
      round
      v-model="showBottom"
      position="bottom"
      :style="{ height: '1.6rem' }"
    >
      <div class="popup-header">
        <div class="title">创建分组</div>
        <div class="btn">确定</div>
      </div>
      <div class="popup-content">
        <div class="img-box">
          <img src="@/../public/images/group_default.png" alt="" />
        </div>
        <div class="input-box">
          <input type="text" v-model.trim="text" @input="textInput" />
          <div class="text">{{ text.length }}/16</div>
        </div>
      </div>
    </van-popup>
  </div>
</template>

<script>
import GroupItem from "@/components/collection/GroupItem.vue";
export default {
  components: {
    GroupItem,
  },
  data: () => {
    return {
      group: [],
      showBottom: false,
      text: "",
    };
  },
  methods: {
    textInput() {
      if (this.text.length >= 16) {
        this.text = this.text.slice(0, 16);
      }
    },
    addGroup() {
      if(this.text=='')return;
      this.group = JSON.parse(localStorage.getItem("myGroup")) || [];
      this.group = [{ title: this.text,id:Date.now() }, ...this.group];
      localStorage.setItem("myGroup", JSON.stringify(this.group));
    },
    deleteGroupItem(item){
      console.log(111);
      this.group = this.group.filter(groupItem=>groupItem.id!=item.id);
      localStorage.setItem("myGroup", JSON.stringify(this.group));
    }
  },
  activated() {
    this.group = JSON.parse(localStorage.getItem("myGroup")) || [];
  },
};
</script>

<style lang="scss" scoped>
.all {
  width: 100%;
  .group {
    width: 100%;
    box-sizing: border-box;
    padding: 0.05rem;
    background-color: var(--bgc-gray2);
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    .add-item {
      width: 1.8rem;
      height: 2.34rem;
      border-radius: 0.12rem;
      overflow: hidden;
      background-color: var(--bgc-white);
      box-sizing: border-box;
      padding: 0.08rem;
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      .icon-add {
        position: relative;
        color: var(--text-icon-yellow);
        font-size: 0.3rem;
        line-height: 0.3rem;
        text-align: center;
        &::after {
          content: "";
          width: 50%;
          height: 50%;
          position: absolute;
          left: 0;
          top: 0;
          right: 0;
          bottom: 0;
          margin: auto;
          background-color: var(--black);
          z-index: -1;
        }
      }
      .text {
        font-size: 0.15rem;
        color: var(--text-gray);

        margin-top: 0.1rem;
      }
    }
  }
  .popup-header {
    position: relative;
    width: 100%;
    height: 0.55rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-sizing: border-box;
    padding: 0 0.15rem;
    border-bottom: 0.01rem solid var(--bgc-gray);
    .title {
      font-size: 0.2rem;
    }
    .btn {
      color: var(--text-gray);
      font-size: 0.16rem;
    }
  }
  .popup-content {
    display: flex;
    box-sizing: border-box;
    padding: 0.15rem;
    .img-box {
      width: 0.62rem;
      min-width: 0.62rem;
      height: 0.62rem;
      border-radius: 0.04rem;
      overflow: hidden;
    }
    .input-box {
      position: relative;
      flex-grow: 1;
      min-height: 100%;
      border-radius: 0.04rem;
      overflow: hidden;
      margin-left: 0.15rem;
      > input {
        width: 100%;
        height: 100%;
        font-size: 0.14rem;
        text-indent: 0.14rem;
        line-height: 0.16rem;
        border: none;
        outline: none;
        background-color: var(--bgc-gray);
      }
      .text {
        position: absolute;
        right: 0.04rem;
        bottom: 0.04rem;
        font-size: 0.14rem;
      }
    }
  }
}
</style>
<style lang="scss">
.all {
  .van-popup__close-icon--top-right {
    font-size: 0.25rem;
    color: transparent;
  }
}
</style>